package scenic.path;

import java.awt.Rectangle;

import scenic.geom.Affine2;
import scenic.geom.Double2;

/**
 * This class defines a cubic Bezier curve. This class does not 
 * define the start point of the curve. The start point is the 
 * end point of the previous segment.
 */
public class CubicBezierSegment extends CurvedSegment {
	private double x1, y1;
	private double x2, y2;
	private double x3, y3;
	
	/**
	 * Creates a cubic bezier segment with the given parameters. The
	 * first control point is not defined here, it is defined by the end
	 * point of the previous segment.
	 * 
	 * @param x1 x coordinate of second control point.
	 * @param y1 y coordinate of second control point.
	 * @param x2 x coordinate of third control point.
	 * @param y2 y coordinate of third control point.
	 * @param x3 x coordinate of fourth control point.
	 * @param y3 y coordinate of fourth control point.
	 */
	public CubicBezierSegment(double x1, double y1, 
			double x2, double y2, 
			double x3, double y3) {
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
		this.x3 = x3;
		this.y3 = y3;
	}
	
	/**
	 * Gets the x coordinate of second control point.
	 */
	public double getX1() {
		return x1;
	}

	/**
	 * Gets the y coordinate of second control point.
	 */
	public double getY1() {
		return y1;
	}

	/**
	 * Gets the x coordinate of third control point.
	 */
	public double getX2() {
		return x2;
	}

	/**
	 * Gets the y coordinate of third control point.
	 */
	public double getY2() {
		return y2;
	}

	/**
	 * Gets the x coordinate of fourth control point.
	 */
	public double getX3() {
		return x3;
	}

	/**
	 * Gets the y coordinate of fourth control point.
	 */
	public double getY3() {
		return y3;
	}

	public Double2 calcPoint(double t, Double2 position) {
		double it = 1.0 - t;
		double a1 = it * it * it;
		double a2 = 3.0 * t * it * it;
		double a3 = 3.0 * t * t * it;
		double a4 = t * t * t;
		Double2 point = new Double2();
		
		point.x = a1 * position.x + a2 * x1 + a3 * x2 + a4 * x3;
		point.y = a1 * position.y + a2 * y1 + a3 * y2 + a4 * y3;
		
		return point;
	}

	public void getBounds(Affine2 transform, Rectangle r, Double2 position) {
		Path.addPointToBounds(transform, r, x1, y1);
		Path.addPointToBounds(transform, r, x2, y2);
		Path.addPointToBounds(transform, r, x3, y3);
		position.x = x3;
		position.y = y3;	
	}
}
